package com.example.generic_;

import java.util.*;

/**
 * 泛型练习: 距离说明, 泛型在HashSet, HashMap的使用情况
 * 1. 创建3个学生对象
 * 2. 放入到HashSet中, 使用
 * 3. 放入到HashMap中, 要求Key是String name, Value 就是学生对象
 * 3. 使用两种方式遍历
 */
public class GenericExercise {
    public static void main(String[] args) {
        Student student1 = new Student("小明", 20);
        Student student2 = new Student("小强", 20);
        Student student3 = new Student("小芳", 20);
        HashSet<Student> hashSet = new HashSet<>();
        hashSet.add(student1);
        hashSet.add(student2);
        hashSet.add(student3);
        for (Student student : hashSet) {
            System.out.println(student);
        }
        HashMap<String, Student> hashMap = new HashMap<>();
        hashMap.put(student1.getName(), student1);
        hashMap.put(student2.getName(), student2);
        hashMap.put(student3.getName(), student3);
        //遍历
        for (String key : hashMap.keySet()) {
            System.out.println(key + " - " + hashMap.get(key));
        }
        //迭代器
        Set<Map.Entry<String, Student>> entries = hashMap.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next = iterator.next();
            System.out.println(next);
        }
    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}